cc.Class({
    extends: cc.Component,

    properties: {
        handTilePrefab : cc.Prefab,
        winTilePrefab : cc.Prefab
        // foo: {
        //    default: null,      // The default value will be used only when the component attaching
        //                           to a node for the first time
        //    url: cc.Texture2D,  // optional, default is typeof default
        //    serializable: true, // optional, default is true
        //    visible: true,      // optional, default is true
        //    displayName: 'Foo', // optional
        //    readonly: false,    // optional, default is false
        // },
        // ...
    },

    // use this for initialization
    onLoad: function () {
        this.tilesArr = [];
        this.handTile = [];
        this.meldCount = 0;
    },

    setPanelPos : function () {
        this.meldCount++;
        this.node.x = this.node.x - 22;
        this.node.y = this.node.y + 104;
        if(this.meldCount == 1){
            this.node.x += 18;
            this.node.y -= 32;
        }
    },

    delTile : function (len) {
        var end = this.tilesArr.length - len;
        for(var i = this.tilesArr.length - 1; i >= end; i--){
            this.tilesArr[i].destroy();
            this.tilesArr.splice(i, 1);
        }

        for(var i in this.handTile){
            this.handTile[i].getComponent("RightTile").moveLeft(len, true);
        }

        this.tilesArr = this.tilesArr.concat(this.handTile);
        this.handTile.length = 0;
    },

    moveTile : function () {
        var handTile = this.tilesArr.splice(-1, 1)[0];
            handTile.getComponent("RightTile").change();

        this.handTile.push(handTile);
    },

    dealer : function () {
        for(var i = 0; i < 13; i++){
            var tile = cc.instantiate(this.handTilePrefab);
                tile.getComponent("RightTile").init(i, false);

            this.tilesArr.push(tile);
            this.node.addChild(tile);
        }
    },

    mo : function (len) {
        len = len || 1;
        for(var i = 0; i < len; i++){
            var tile = cc.instantiate(this.handTilePrefab);
                tile.getComponent("RightTile").init(this.node.childrenCount, true);

            this.handTile.push(tile);
            this.node.addChild(tile);
        }
    },

    discard : function (MJIds) {
        var startPos = [];
        for(var i = MJIds.length - 1; i >= 0; i--){
            var tile = this.handTile[i];
            tile.destroy();
            startPos.push(tile.convertToWorldSpace(cc.p(0, 0)));
            this.handTile.splice(i, 1);
        }

        return startPos;
    },

    sort : function (act, MJIds, type) {
        switch(act){
            case "chi":
            case "peng":
                this.setPanelPos();
                this.delTile(2);
                this.moveTile();
                break;
            case "gang":
                switch(type){
                    case 1:
                        this.setPanelPos();
                        this.delTile(3);
                        break;
                    case 2:
                        this.setPanelPos();
                        this.delTile(4);
                        break;
                    case 3:
                        this.delTile(1);
                        break;
                }
                break;
        }
    },

    hu : function (handIds, moIds) {
        var childrenCount = 0;
        this.tilesArr.length = 0;
        this.handTile.length = 0;

        this.node.x -= 5;
        // this.node.y += 17;

        for(var i in this.node.children){
            this.node.children[i].destroy();
        }

        for(var i in handIds){
            var tile = cc.instantiate(this.winTilePrefab);
                tile.getComponent("RightWin").init(childrenCount, false, handIds[i]);

            this.node.addChild(tile);
            childrenCount++;
        }

        for(var i in moIds){
            var tile = cc.instantiate(this.winTilePrefab);
                tile.getComponent("RightWin").init(childrenCount, true, moIds[i]);

            this.node.addChild(tile);
            childrenCount++;
        }
    }
});
